<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
</head>

<body>
    <form>
        <div>
            <label for="username">用户名：</label>
            <input type="text" name="username" id="username" />
        </div>
        <div>
            <label for="password">密码：</label>
            <input type="password" name="password" id="password" />
        </div>
        <div>
            <input id="btn" type="button" value="提交" />
        </div>
    </form>
    <script>
        var username = document.getElementById('username');
        var password = document.getElementById('password');
        var btn = document.getElementById('btn');
        btn.onclick = function() {
            var user_v = username.value;
            var pasd_v = password.value;
            console.log(user_v, pasd_v);
            var obj =
                'bookname=' + user_v + '&author=' + pasd_v + '&publisher=hello';
            var xhr = new XMLHttpRequest();
            xhr.open('post', 'http://www.liulongbin.top:3006/api/addbook');
            xhr.setRequestHeader(
                'content-type',
                'application/x-www-form-urlencoded'
            );
            xhr.send(obj);
            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    var ret = xhr.response;
                    // var objs = JSON.parse(ret);
                    console.log(ret);
                }
            };
        };
    </script>
</body>

</html>